<script setup>
import { ref } from 'vue'




const userPassword =  ref({
    old_pwd : '',
    new_pwd: '',
    re_pwd: ''
})


//定义校验密码函数,直接定义要写在校验规则前
const checkRePassword = (rule,value,callback)=>{
    if(value === ''){
        callback(new Error("请确认密码"))
    }else if(value !== userPassword.value.new_pwd){
        callback(new Error("两次密码不相同"))
    }else{
        callback()
    }
}

//定义前端的校验规则
const rules = {
    old_pwd : [
        {required: true, message: "请输入密码",trigger: 'blur'},
        {min: 5,max: 16,message: "长度为5~16的非空字符串",trigger: 'blur'}
    ],
    new_pwd:[
        {required: true, message: "请输入密码",trigger: 'blur'},
        {min: 5,max: 16,message: "长度为5~16的非空字符串",trigger: 'blur'}
    ],
    re_pwd:[
        {validator: checkRePassword,trigger:'blur'}
    ]
}


import {ElMessage} from 'element-plus'
import {userPasswordUpdateService} from '@/api/user.js'
import router from '@/router';
const updateUserPassword = async ()=>{
    const result = await userPasswordUpdateService(userPassword.value);
    ElMessage.success(result.message? result.message:'修改成功')

    router.push('/login')

}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>修改密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userPassword"  label-width="100px" size="large" :rules="rules">
                    <el-form-item label="原密码" prop = "old_pwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="userPassword.old_pwd" ></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop = "new_pwd">
                        <el-input  type="password" placeholder="请输入密码" v-model="userPassword.new_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop = "re_pwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码" v-model="userPassword.re_pwd"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserPassword">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>

